﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace MyDefaults.Core
{
    public class Updater
    {
        private readonly object _lock = new object();

        private Thread _updaterThread;
        private bool _isUpdating;

        public void Start()
        {
            lock (_lock)
            {
                _isUpdating = true;

                _updaterThread = new Thread(UpdaterThread);
                _updaterThread.Start();
            }
        }

        public void Stop()
        {
            lock (_lock)
            {
                _isUpdating = false;

                _updaterThread.Join();
            }
        }

        private void UpdaterThread()
        {
            MySettings settings = new MySettings();
            RegistryHelper regHelper = new RegistryHelper();

            while (_isUpdating)
            {
                settings.Update();
                regHelper.ApplyChanges(settings.RegistryEntries);

                Thread.Sleep(settings.UpdateInterval);
            }
        }
    }
}
